<?php
	$url = "http://www.wowarmory.com/character-sheet.xml?r=Proudmoore&n=Kallalilly&rhtml=n";	//rhtml=n forces XML
	if(!isset($rootdir)) $rootdir = "/home/nrb62281/public_html";
	include_once($rootdir . "/wowsig/DataManagers/DataManager.php");
	$dataManager = new dataManager;
	
	$IMG_WIDTH = 400;
	$IMG_HEIGHT = 64;
	$_DEFAULT_ICON = $rootdir . "/wowsig/_images/portraits/" . "defaulticon.gif"; //64x64 image gif
	$TEXT_AREA_START_X_COORD = 66;
	
	//http://nathansinfo.com/wowsig/sig.php?c=Kallalilly&r=Proudmoore
	
	//http://nathansinfo.com/wowsig/sig.php?c=Kallalilly&r=Proudmoore&l1=spec&l2=hp,mp,ap,crit
	
	
	if(!isset($_GET["c"])) {$_GET["c"] = "";}
	if(!isset($_GET["r"])) {$_GET["r"] = "";}
	
	$characterName = $_GET["c"];
	$realmName = $_GET["r"];
	try{
		$characterDataObject = $dataManager->getCharacterInfoByNameRealmRegion($characterName, $realmName, "us");
	}catch(Exception $error){
		error_log($error->getFile() . ":" . $error->getLine() . "\n" .$error->getMessage() . "\n" . $error->getTraceAsString());
	}
	$img = imagecreate($IMG_WIDTH,$IMG_HEIGHT);
	//$backgroundColor = imagecolorallocatealpha($img,0,0,0, 127); // black, but fully transparent
	//$backgroundColor = imagecolorallocatealpha($img,0,0,0, 63); // black, but half visible
	$backgroundColor = imagecolorallocatealpha($img,0,0,0,0); // black, but fully visible
	$textColor = imagecolorallocate($img,255,255,255); //black
	$urlTextColor = imagecolorallocate($img,246,199,42);
	if($characterDataObject == null){
		$characterDataObject = new characterData;
		$characterDataObject->setNameCharacter($characterName);
//		$characterDataObject->setGuildName("Missing");
//		$characterDataObject->setRealmName($realmName);
//		$characterDataObject->setLevel("1");
		
//		$characterDataObject->setClass("Warrior");
//		$characterDataObject->setGender("male");
//		$characterDataObject->setRace("Human");
		$nameLine = $characterDataObject->getNameCharacter();
		ImageTTFText($img, "14", "0", 66, 18, $textColor, "{$rootdir}/wowsig/_fonts/MORPHEUS.TTF", $nameLine);
		
		// DEFAULT VALUE! Replace
	}else{
		
		
		// imagestring(imageresource, font (int), x(int), y(int), color (string) (text), color(int)
		// font
		// 		- Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where higher numbers corresponding to larger fonts) 
		//		  or any of your own font identifiers registered with imageloadfont().

		//imagestring($img, 5, $TEXT_AREA_START_X_COORD, 2, 
		//	$characterDataObject->getNamePrefix() . $characterDataObject->getNameCharacter() . $characterDataObject->getNameSuffix() , 
		//	$textColor);
		
		$line2 = "";	
			
		if(strlen($characterDataObject->getGuildName()) > 0) { $line2 .= "<" . $characterDataObject->getGuildName() . "> of "; }
		
		$line2 .= $characterDataObject->getRealmName();
		
		//imagestring($img, 5, $TEXT_AREA_START_X_COORD, 19, $line2, $textColor); 
		
		//imagestring($img, 5, $TEXT_AREA_START_X_COORD, 33, "test3", $textColor);
		//imagestring($img, 5, $TEXT_AREA_START_X_COORD, 47, "test4", $textColor);
		
		//addTextToImage("{$rootdir}/wowsig/_fonts/MORPHEUS.TTF","14","0", $textColor, $characterDataObject->getNamePrefix() . $characterDataObject->getNameCharacter() . $characterDataObject->getNameSuffix(), $img);
		$nameLine = $characterDataObject->getNamePrefix() . $characterDataObject->getNameCharacter() . $characterDataObject->getNameSuffix();
		ImageTTFText($img, "14", "0", 66, 18, $textColor, "{$rootdir}/wowsig/_fonts/MORPHEUS.TTF", $nameLine);
		
		addTextToImage("{$rootdir}/wowsig/_fonts/FRIZQT__.TTF","9","0", $textColor, $line2, $img);
		addTextToImage("{$rootdir}/wowsig/_fonts/FRIZQT__.TTF","9","0", $textColor, "Level " . $characterDataObject->getLevel() . " " . $characterDataObject->getClass(), $img);

		
		
		$font = "{$rootdir}/wowsig/_fonts/FRIZQT__.TTF";
		$fontsize = "7";
		$fontangle = "0";
		$text = "http://www.nathansinfo.com/wowsig/";
		
		$box = imageTTFBbox($fontsize, $fontangle, $font, $text);
		$textwidth = abs($box[4] - $box[0]);
		$textheight = abs($box[5] - $box[1]);
		$xcoord = $IMG_WIDTH - $textwidth - 2;
		$ycoord = $IMG_HEIGHT - 5;
		
		ImageTTFText($img, $fontsize, $fontangle, $xcoord, $ycoord, $urlTextColor, $font, $text);
		
		$icon = $rootdir . "/wowsig/_images/portraits/" . $characterDataObject->getRace() . "/" . 
			$characterDataObject->getClass() . "/" . $characterDataObject->getGender() . "/";
		
		$characterLevel = int($characterDataObject->getLevel());
		if($characterLevel < 60){
			$icon .= "1.gif";
		}elseif($characterLevel < 70){
			$icon .= "60.gif";
		}elseif($characterLevel < 80){
			$icon .= "70.gif";
		}else{
			$icon .= "80.gif";
		}
		if(file_exists($icon)){
			imagecopy($img, imagecreatefromgif($icon), 0, 0, 0, 0, 64, 64);
		}else{
			//imagecopy($img, imagecreatefromgif($_DEFAULT_ICON), 0, 0, 0, 0, 64, 64);
		}
	}
	
	header('Content-Type: image/png');
	imagepng($img);
	imagedestroy($img);	
	
function addTextToImage($fontPath, $fontSize, $fontAngle, $fontColor, $text, $image){
	
	static $ycoord = 22;
	$ybuffer = 4;
	$xcoord = 66;
	
	$box = imageTTFBbox($fontSize, $fontAngle, $fontPath, $text);
	$textwidth = abs($box[4] - $box[0]);
	$textheight = abs($box[5] - $box[1]);
	
	//$ycoord += $textheight;
	$ycoord += 10;
	
	ImageTTFText($image, $fontSize, $fontAngle, $xcoord, $ycoord, $fontColor, $fontPath, $text );
	
	$ycoord += $ybuffer;
	
}	
?>